package com.taxi.driver.common;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Window;
import android.view.WindowManager;

import com.qianxx.base.LibBaseActivity;
import com.taxi.driver.R;
import com.taxi.driver.common.dagger.AppComponent;
import com.taxi.driver.widget.SystemBarTintManager;

/**
 * 功能描述：因"使用字体图片"会导致"高德地图的AMapNaviView"不能正常使用，增加该类
 */
public class BaseActivityWithoutIconics extends LibBaseActivity {

    private PowerManager pm;
    private PowerManager.WakeLock wl;
    private AudioManager mAudioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //设置状态栏的样式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(getBarColor());
        }

        //管理屏幕常亮状态
        pm = (PowerManager) getSystemService(POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
                getResources().getString(R.string.app_name));

        //管理系统音量
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 获取状态栏颜色
     *
     * @return
     */
    public int getBarColor() {
        return R.color.theme_color;
    }

    /**
     * 屏幕常亮
     */
    public void bright() {
        try {
            wl.acquire();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消常亮
     */
    public void cancelBright() {
        try {
            wl.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getSystemVolume() {
        return mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
    }

    public int getMaxSystemVolume() {
        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
    }

    public AppComponent getAppComponent() {
        return Application.getAppComponent();
    }

}
